<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="部门" prop="deptId">
        <treeselect size="small" style="width: 200px;" v-model="queryParams.deptId" :options="deptOptions"
                    :show-count="true"
                    placeholder="请选择归属部门"/>
      </el-form-item>
      <el-form-item label="标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择类型" clearable>
          <el-option
            v-for="dict in dict.type.show_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="置顶" prop="setTop">
        <el-select v-model="queryParams.setTop" placeholder="请选择是否置顶" clearable>
          <el-option
            v-for="dict in dict.type.article_top"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="small" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="small" @click="resetQuery">重置</el-button>
        <el-button type="success" icon="el-icon-plus" size="small" @click="handleAdd"
                   v-hasPermi="['applet:activityShow:add']">新增
        </el-button>
      </el-form-item>
    </el-form>


    <el-table border stripe v-loading="loading" :data="activityShowList" @selection-change="handleSelectionChange">
      <!--      <el-table-column type="selection" width="55" align="center"/>-->
      <el-table-column width="80" label="编号" align="center" show-overflow-tooltip prop="id"/>
      <el-table-column label="标题" align="center" prop="title" show-overflow-tooltip/>
      <el-table-column label="浏览" width="80" align="center" prop="readSum">

      </el-table-column>
      <el-table-column label="列表图片" align="center" prop="url" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.img" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="类型" width="100" align="center" prop="type">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.show_type" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <el-table-column label="发布部门" width="120" show-overflow-tooltip align="center" prop="deptName"/>
      <el-table-column label="置顶" width="80" align="center" prop="setTop">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.article_top" :value="scope.row.setTop"/>
        </template>
      </el-table-column>
      <el-table-column label="状态" width="80" align="center" prop="status">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            :active-value="0"
            :inactive-value="1"
            @change="handleStatusChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="发布类型" width="100" align="center" prop="releaseType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.release_type" :value="scope.row.releaseType"/>
        </template>
      </el-table-column>
      <el-table-column label="发布时间" align="center" prop="releaseTime" width="160"/>
      <el-table-column label="更新时间" align="center" prop="updateTime" width="160"/>
      <el-table-column label="更新账号" align="center" prop="updateBy" width="120"/>
      <el-table-column label="备注说明 " align="center" width="120" show-overflow-tooltip prop="remark"/>
      <el-table-column label="操作" width="130" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['applet:activityShow:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['applet:activityShow:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改活动风采对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1350px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">

        <el-row :gutter="15">
          <el-col :span="6">
            <el-form-item label="类型" prop="type">
              <el-select v-model="form.type" placeholder="请选择类型">
                <el-option v-for="dict in dict.type.show_type" :key="dict.value" :label="dict.label"
                           :value="parseInt(dict.value)"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="是否置顶" prop="setTop">
              <el-radio-group v-model="form.setTop">
                <el-radio-button
                  v-for="dict in dict.type.article_top"
                  :key="dict.value"
                  :label="parseInt(dict.value)"
                >{{ dict.label }}
                </el-radio-button>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="发布类型" prop="releaseType">
              <el-radio-group v-model="form.releaseType">
                <el-radio-button
                  v-for="dict in dict.type.release_type"
                  :key="dict.value"
                  :label="parseInt(dict.value)"
                >{{ dict.label }}
                </el-radio-button>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item v-if="form.releaseType != 0" label="发布时间" prop="releaseTime">
              <el-date-picker clearable :disabled="form.releaseType == 0"
                              v-model="form.releaseTime"
                              type="datetime"
                              value-format="yyyy-MM-dd HH:mm:ss"
                              placeholder="请选择发布时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="标题" prop="title">
              <el-input v-model="form.title" placeholder="请输入标题"/>
            </el-form-item>
            <el-form-item label="备注说明 " prop="remark">
              <el-input v-model="form.remark" placeholder="请输入备注说明 "/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="列表图片" prop="img">
              <image-upload :limit="1" v-model="form.img"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="内容" prop="content">
          <editor v-model="form.content" :min-height="192"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  addActivityShow,
  changeActivityShowStatus,
  delActivityShow,
  getActivityShow,
  listActivityShow,
  updateActivityShow
} from "@/api/background/activityShow";
import {deptTreeSelect} from "@/api/system/user";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "ActivityShow",
  dicts: ['show_type', 'article_top', 'release_type'],
  components: {Treeselect},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 活动风采表格数据
      activityShowList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
        type: null,
        deptId: null,
        setTop: null,
        status: null,
        releaseType: null,
      },
      deptOptions: [],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        title: [
          {required: true, message: "标题不能为空", trigger: "blur"},
          {min: 10, max: 40, message: "标题10到40个字", trigger: "blur"}
        ],
        type: [
          {required: true, message: "请选择类型", trigger: "change"}
        ],
        setTop: [
          {required: true, message: "请选择是否置顶", trigger: "change"}
        ],
        status: [
          {required: true, message: "请选择是否上架", trigger: "change"}
        ],
        releaseType: [
          {required: true, message: "请选择发布类型", trigger: "change"}
        ],
        releaseTime: [
          {required: true, message: "发布时间不能为空", trigger: "blur"}
        ],
        img: [
          {required: true, message: "列表图片不能为空", trigger: "blur"}
        ],
      }
    };
  },
  created() {
    this.getDeptTree()
    this.getList();
  },
  methods: {
    /** 查询部门下拉树结构 */
    getDeptTree() {
      deptTreeSelect().then(response => {
        this.deptOptions = response.data;
      });
    },
    /** 查询活动风采列表 */
    getList() {
      this.loading = true;
      listActivityShow(this.queryParams).then(response => {
        this.activityShowList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        title: null,
        type: null,
        deptId: null,
        content: null,
        setTop: 0,
        status: 0,
        releaseType: 0,
        releaseTime: null,
        remark: null,
        img: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加活动风采";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getActivityShow(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改活动风采";
      });
    },
    handleStatusChange(row) {
      let text = row.status === "0" ? "上架" : "下架";
      this.$modal.confirm('确认要"' + text + '""' + row.title + '"的文章吗？').then(function () {
        return changeActivityShowStatus(row.id, row.status);
      }).then(() => {
        this.$modal.msgSuccess(text + "成功");
        this.handleQuery()
      }).catch(function () {
        row.status = row.status === "0" ? "1" : "0";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateActivityShow(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addActivityShow(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除活动风采编号为"' + ids + '"的数据项？').then(function () {
        return delActivityShow(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('applet/activityShow/export', {
        ...this.queryParams
      }, `activityShow_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
